home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / VideoToolbox 95.04.18 / VideoToolboxSources / kbhit.c < prev    next >
Text File  |  1995-03-21  |  2KB  |  66 lines

  1. /*
  2. kbhit.c
  3. kbhit() returns 1 if a keypress awaits processing, 0 otherwise.
  4. getcharUnbuffered() always returns immediately. It returns value -1 if there's 
  5. no character to return.
  6.  
  7. getcharUnbuffered() is used by Choose.c
  8.  
  9. 6/13/90    dgp wrote kbhit, based on suggestion from Michael Kahl and an earlier version by
  10.         Evan Relkin.
  11. 12/25/93 dgp cosmetic editing
  12. 8/1/94 dgp added support for MetroWerks CodeWarrior C 3.5.
  13. 10/1/94 dgp updated for MetroWerks CodeWarrior C 4.5, which still doesn't support
  14.         unbuffered input from the console.
  15. 10/8/94    dgp Discovered that GetNextEvent() works fine for unbuffered input.
  16. 1/7/95    dgp Updated to take advantage of the CW5 SIOUX console's new ability to handle an event.
  17. */
  18. #include "VideoToolbox.h"
  19. #if (THINK_C || THINK_CPLUS)
  20.     #include <console.h>
  21. #endif
  22. #if __MWERKS__
  23.     #include <console.h>
  24.     #include <SIOUX.h>
  25. #endif
  26.  
  27. int kbhit(void)
  28. {
  29.     #if (THINK_C || THINK_CPLUS)
  30.         int c;
  31.     
  32.         c=getcharUnbuffered();
  33.         if(c==EOF)return 0;
  34.         ungetc(c,stdin);
  35.         return 1;
  36.     #else
  37.         EventRecord event;
  38.         
  39.         return EventAvail(keyDownMask,&event);
  40.     #endif
  41. }
  42.  
  43. int getcharUnbuffered(void)
  44. {
  45.     int c;
  46.     #if (THINK_C || THINK_CPLUS)
  47.  
  48.         csetmode(C_RAW,stdin);        /* unbuffered: no echo, no editing */
  49.         c=getchar();
  50.         csetmode(C_ECHO,stdin);        /* default mode: line-buffered, echo, full editing */
  51.     #else
  52.         EventRecord event;
  53.  
  54.         while(!GetNextEvent(keyDownMask,&event)){
  55.             #if __MWERKS__
  56.                 // Allow the console to respond to the mouse, e.g. drag, zoom, or resize.
  57.                 if(GetNextEvent(mDownMask|mUpMask,&event))SIOUXHandleOneEvent(&event);
  58.             #endif
  59.         }
  60.         c=event.message&charCodeMask;
  61.         if(c=='.' && (event.modifiers&cmdKey))abort();
  62.     #endif
  63.     return c;
  64. }
  65.  
  66.